def read(filepath):
    try:
        handle = open(filepath)
        content = handle.read()
        handle.close()
    except IOError:
        raise IOError
    return content


def write(filepath, contents):
    handle = open(filepath, 'w')
    handle.write(contents)
    handle.close()
    try:
        if read(filepath) is contents:
            return True
        else:
            return False
    except IOError:
        raise IOError("No such file or directory")


def addtofile(filepath, contents):
    handle = open(filepath, 'a')
    handle.write(contents)
    handle.close()


def get_input(prompt="> "):
    return raw_input(prompt)


def split(text):
    split = []
    i = 0
    br = i
    brackets = False

    while i < len(text):
        if not brackets:
            if text[i] == '"':
                brackets = True
                split.append(text[br:i])
                br = i + 1
            elif text[i] == ' ':
                split.append(text[br:i])
                br = i + 1
        else:
            if text[i] == '"':
                split.append(text[br:i])
                brackets = False
                br = i + 1
        i += 1
    split.append(text[br:])

    splitted = []
    i = 0
    while i < len(split):
        if split[i] != '':
            splitted.append(split[i])
        i += 1
    return splitted


def search(li, items):
    i = 0
    while i < len(li):
        if type(items) is list:
            if li[i] in items:
                return i
        else:
            if li[i] == items:
                return i
        i += 1
    return -1


def findall(li, items):
    i = 0
    found = []
    while i < len(li):
        if type(items) is list:
            if li[i] in items:
                found.append(i)
        elif type(items) is str:
            if li[i] == items:
                found.append(i)
        else:
            raise TypeError("list or str expected")
        i += 1
    if found:
        return found
    else:
        return -1


